
package bbchess.dialogs;

import bbchess.GameManager;

/**
 * NewGamePVCDialog: This dialog displays options to play vs a computer.
 * @author Tyler Eastman
 */
public class NewGamePVCDialog extends ChessDialog {

    // Initialize variables
    GameManager gameManager;
    private int depth;
    NewGameDialog newGameDialog;

    /** Creates new form ChatDialog */
    public NewGamePVCDialog(java.awt.Frame parent, boolean modal, GameManager gameManager, NewGameDialog newGameDialog) {
        super(parent, modal, true);
        this.gameManager = gameManager;
        this.newGameDialog = newGameDialog;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startGameButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        playerColor = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        player1Name = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        computerColor = new javax.swing.JLabel();
        computerStrengthSlider = new javax.swing.JSlider();
        jLabel6 = new javax.swing.JLabel();
        computerStrengthLabel = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Player vs Computer");
        setResizable(false);

        startGameButton.setText("Start Game");
        startGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startGameButtonActionPerformed(evt);
            }
        });
        startGameButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                startGameButtonKeyPressed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Player"));

        jLabel2.setText("Color:");

        playerColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "White", "Black" }));
        playerColor.setSelectedItem(playerColor);
        playerColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playerColorActionPerformed(evt);
            }
        });
        playerColor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                playerColorKeyPressed(evt);
            }
        });

        jLabel1.setText("Your Name: ");

        player1Name.setText("Player1");
        player1Name.selectAll();
        player1Name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                player1NameActionPerformed(evt);
            }
        });
        player1Name.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                player1NameKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(57, 57, 57)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(playerColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(player1Name, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(player1Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(playerColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Computer"));

        jLabel3.setText("Color:");

        computerColor.setText("Black");

        computerStrengthSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                computerStrengthSliderStateChanged(evt);
            }
        });

        jLabel6.setText("Playing Strength:");

        computerStrengthLabel.setText("Moderate");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(91, 91, 91)
                .addComponent(computerColor))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel6)
                .addGap(37, 37, 37)
                .addComponent(computerStrengthLabel))
            .addComponent(computerStrengthSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(computerColor))
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(computerStrengthLabel))
                .addGap(6, 6, 6)
                .addComponent(computerStrengthSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(backButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
                        .addComponent(startGameButton))
                    .addComponent(jPanel2, 0, 263, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startGameButton)
                    .addComponent(backButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void player1NameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_player1NameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_player1NameActionPerformed

    private void playerColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playerColorActionPerformed
        if (playerColor.getSelectedIndex() == 0) {
            computerColor.setText("Black");
        }
        else {
            computerColor.setText("White");
        }
    }//GEN-LAST:event_playerColorActionPerformed

    private void startGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startGameButtonActionPerformed
        startGame();
    }//GEN-LAST:event_startGameButtonActionPerformed

    private void computerStrengthSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_computerStrengthSliderStateChanged
        int value = computerStrengthSlider.getValue();

        if (value <= 25) {
            computerStrengthLabel.setText("Weak");
            depth = 1;
        }

        else if (value <= 50) {
            computerStrengthLabel.setText("Moderate");
            depth = 4;
        }

        else if (value <= 75) {
            computerStrengthLabel.setText("Strong");
            depth = 8;
        }

        else if (value <= 99) {
            computerStrengthLabel.setText("Very Strong");
            depth = 10;
        }

        else {
            computerStrengthLabel.setText("Strongest");
            depth = 12;
        }
    }//GEN-LAST:event_computerStrengthSliderStateChanged

    private void player1NameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_player1NameKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            playerColor.requestFocus();
        }
    }//GEN-LAST:event_player1NameKeyPressed

    private void playerColorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_playerColorKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            startGameButton.requestFocus();
        }
    }//GEN-LAST:event_playerColorKeyPressed

    private void startGameButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_startGameButtonKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            startGame();
        }
    }//GEN-LAST:event_startGameButtonKeyPressed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        setVisible(false);
        newGameDialog.setVisible(true);
}//GEN-LAST:event_backButtonActionPerformed

    public void startGame() {
        if (playerColor.getSelectedIndex() == 0) {
            gameManager.setWhiteName(player1Name.getText());
            gameManager.setBlackName("BlackBox");
            gameManager.setComputer("black");
        }
        else {
            gameManager.setBlackName(player1Name.getText());
            gameManager.setWhiteName("BlackBox");
            gameManager.setComputer("white");
        }

        gameManager.newGamePVC(depth);
        dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JLabel computerColor;
    private javax.swing.JLabel computerStrengthLabel;
    private javax.swing.JSlider computerStrengthSlider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField player1Name;
    private javax.swing.JComboBox playerColor;
    private javax.swing.JButton startGameButton;
    // End of variables declaration//GEN-END:variables

}
